package keshe.view;


import keshe.ACCOUNT;
import keshe.MyObjectInputStream;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import java.util.ArrayList;

public class LoginJFrame extends JFrame implements MouseListener, Runnable {
    private TextField name = new TextField("银行账户");
    private JPasswordField password = new JPasswordField("");
    private JButton register = new JButton(new ImageIcon("image/register/注册按钮.png"));
    private JButton login = new JButton(new ImageIcon("image/login/登录按钮.png"));
    private JButton see=new JButton(new ImageIcon("src/keshe/image/eye.png"));
    private JLabel Welcome = new JLabel(String.format("%40s", "请您进行银行账户登录"));
    private ArrayList<ACCOUNT> accounts = new ArrayList<>();//接收文件中的账户对象

    public LoginJFrame() {
        //读取文件中的账户对象
        ReadAccounts();
        //登录主界面
        InitFrame();
        //滚动欢迎界面
        Thread t = new Thread(this);
        t.start();
    }

    private void ReadAccounts() {
        try {
            MyObjectInputStream ois = new MyObjectInputStream(new FileInputStream("AccountData/Accounts"));
            //反序列化流读取文件中所有对象
            while (true) {
                try {
                    ACCOUNT account = new ACCOUNT();
                    account = (ACCOUNT) (ois.readObject());
                    accounts.add(account);
                } catch (EOFException e) {
                    System.out.println("读取完成");
                    break;
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
            ois.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (ACCOUNT account : accounts) {
            System.out.println(account);
        }
    }

    private void InitFrame() {
        this.setSize(400, 287);
        this.setLocationRelativeTo(null);
        this.setTitle("银行登录界面");
        this.setDefaultCloseOperation(3);
        this.setAlwaysOnTop(true);
        this.setLayout(null);
        Welcome.setBounds(100, 25, 200, 25);
        name.setBounds(100, 75, 200, 25);
        password.setBounds(100, 125, 200, 25);
        see.setBounds(300,125,20,25);
        password.setEchoChar('*');
        this.add(Welcome);
        this.add(password);
        this.add(name);
        this.add(see);
        ImageIcon back = new ImageIcon("src/keshe/image/background.jpg");
        JLabel background = new JLabel(back);
        background.setBounds(0, -25, 400, 300);

        //按钮不再填充所有所占的空间
        login.setContentAreaFilled(false);
        login.setBorderPainted(false);
        login.setBounds(80, 200, 100, 25);
        register.setBorderPainted(false);
        register.setContentAreaFilled(false);
        register.setBounds(220, 200, 100, 25);
        see.addMouseListener(this);
        register.addMouseListener(this);
        login.addMouseListener(this);


        this.getContentPane().add(register);
        this.getContentPane().add(login);
        this.getContentPane().add(background);
        this.setVisible(true);
    }

    public static void main(String[] args) throws EOFException {
        LoginJFrame l = new LoginJFrame();
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getSource() == register) {
            register.setIcon(new ImageIcon("image/register/注册按下.png"));
        } else if (e.getSource() == login) {
            login.setIcon(new ImageIcon("image/login/登录按下.png"));
        }
        if(e.getSource()==see){
            password.setEchoChar((char)0);
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (e.getSource() == register) {
            this.dispose();
            new RegisterJFrame();
            register.setIcon(new ImageIcon("image/login/注册按钮.png"));
        } else if (e.getSource() == login) {
            String new_name = name.getText();//银行账户
            String new_passWord = password.getText();
            boolean flash = true;
            for (ACCOUNT account : accounts) {
                //匹配成功
                if (account.getBankCardId().equals(new_name) && account.getPassWord().equals(new_passWord)) {
                    flash = false;
                    showDiglog(account.getUserName() + ",恭喜你，登录成功！！");
                    this.dispose();
                    new BankJFrame(new AccountJPanel(account), account);
                }
            }
            if (flash) showDiglog("用户名或密码输入错误，登陆失败！");
            login.setIcon(new ImageIcon("image/login/登录按钮.png"));
        }
        if(e.getSource()==see){
            password.setEchoChar('*');
        }
    }

    //对话框方法
    public void showDiglog(String messeage) {
        JOptionPane.showMessageDialog(this, messeage);
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    @Override
    public void run() {
        while (true) {
            String str = Welcome.getText();
            Welcome.setText(str.substring(1) + str.substring(0, 1));
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
